# uncompyle6 version 3.2.3
# Python bytecode 3.6 (3379)
# Decompiled from: Python 3.6.8 |Anaconda custom (64-bit)| (default, Feb 21 2019, 18:30:04) [MSC v.1916 64 bit (AMD64)]
# Embedded file name: db\tables\events.py
__author__ = "sanyi"
from sqlalchemy import *
from sqlalchemy.orm import mapper
from sqlalchemy.dialects.postgresql import UUID as C_UUID
from sqlalchemy.dialects.postgresql import JSONB as TYPE_JSONB
from db.tables import metadata
from settings_provider import settings
from helpers.mailer.base_mailer import Mailer
from helpers.setup_types import get_setup_type


class EventSeverity:
    NOTIFICATION = 0
    INFORMATION = 1
    IMPORTANT = 2
    CRITICAL = 3


events = dict()
events["account_created"] = dict(id=0)
events["account_details_modified"] = dict(id=1)
events["account_email_change_asked"] = dict(
    id=2,
    email_template=dict(
        subject=None,
        subject_template="email_change_asked_subject.txt.jinja2",
        html_template="email_change_asked.html.jinja2",
        txt_template="email_change_asked.txt.jinja2",
    ),
)
events["account_email_changed"] = dict(id=3)
events["account_password_reset_asked"] = dict(
    id=4,
    email_template=dict(
        subject=None,
        subject_template="password_reset_asked_subject.txt.jinja2",
        html_template="password_reset_asked.html.jinja2",
        txt_template="password_reset_asked.txt.jinja2",
    ),
)
events["account_password_reset"] = dict(id=5)
events["account_password_changed"] = dict(id=6)
events["account_account_verified"] = dict(id=7)
events["account_login"] = dict(id=8)
events["account_logout"] = dict(id=9)
events["account_otp_reset_asked"] = dict(
    id=10,
    email_template=dict(
        subject=None,
        subject_template="otp_reset_asked_subject.txt.jinja2",
        html_template="otp_reset_asked.html.jinja2",
        txt_template="otp_reset_asked.txt.jinja2",
    ),
)
events["account_otp_reset"] = dict(id=11)
events["account_otp_removed"] = dict(id=12)
events["account_otp_set"] = dict(id=13)
events["child_account_created"] = dict(id=100)
events["child_account_deleted"] = dict(id=101)
events["child_account_modified"] = dict(id=102)
events["child_account_set_access"] = dict(id=103)
events["target_created"] = dict(id=200)
events["target_modified"] = dict(id=201)
events["target_deleted"] = dict(id=202)
events["target_add_allowed_host"] = dict(id=203)
events["target_remove_allowed_host"] = dict(id=204)
events["target_not_responsive"] = dict(id=205, notification=True)
events["target_validated"] = dict(id=206)
events["target_verification_succeeded"] = dict(id=207)
events["target_verification_failed"] = dict(
    id=208,
    notification=True,
    email_template=dict(
        subject=None,
        subject_template="target_verification_failed_subject.txt.jinja2",
        html_template="target_verification_failed.html.jinja2",
        txt_template="target_verification_failed.txt.jinja2",
    ),
)
events["target_continuous_mode_disabled"] = dict(
    id=209,
    notification=True,
    email_template=dict(
        subject=None,
        subject_template="continuous_mode_disabled_subject.txt.jinja2",
        html_template="continuous_mode_disabled.html.jinja2",
        txt_template="continuous_mode_disabled.txt.jinja2",
    ),
)
events["group_created"] = dict(id=300)
events["group_deleted"] = dict(id=301)
events["group_modified"] = dict(id=302)
events["group_targets_modified"] = dict(id=303)
events["scan_scheduled"] = dict(id=400)
events["scan_modified"] = dict(id=401)
events["scan_deleted"] = dict(id=402)
events["scan_started"] = dict(id=403)
events["scan_done"] = dict(
    id=404,
    notification=True,
    email_template=dict(
        subject=None,
        subject_template="scan_done_subject.txt.jinja2",
        html_template="scan_done.html.jinja2",
        txt_template="scan_done.txt.jinja2",
    ),
)
events["scan_failed"] = dict(
    id=405,
    notification=True,
    severity=EventSeverity.IMPORTANT,
    email_template=dict(
        subject=None,
        subject_template="scan_failed_subject.txt.jinja2",
        html_template="scan_failed.html.jinja2",
        txt_template="scan_failed.txt.jinja2",
    ),
)
events["scan_user_aborted"] = dict(id=406)
events["scan_user_paused"] = dict(id=407)
events["scan_user_resumed"] = dict(id=414)
events["scan_mi_required_no_ui_session"] = dict(
    id=408, notifiction=True, severity=EventSeverity.IMPORTANT
)
events["scan_mi_required_scheduled_scan"] = dict(
    id=409, notifiction=True, severity=EventSeverity.NOTIFICATION
)
events["scan_job_starting"] = dict(id=410)
events["scan_job_done"] = dict(id=411)
events["scan_job_failed"] = dict(id=412)
events["scan_job_aborted"] = dict(id=413)
events["scan_job_paused"] = dict(id=415)
events["scan_scanner_event"] = dict(id=420)
events["scan_wvs_crawl_memlimit"] = dict(id=430, notification=True)
events["scan_wvs_aborted"] = dict(id=431, notification=True)
events["scan_wvs_sensor_found"] = dict(id=432)
events["scan_wvs_sensor_not_found"] = dict(id=433)
events["scan_wvs_ls_error"] = dict(id=434)
events["scan_wvs_au_error"] = dict(id=435)
events["scan_wvs_al_error"] = dict(id=436)
events["scan_wvs_crawling"] = dict(id=437)
events["scan_wvs_deep_scan"] = dict(id=438)
events["scan_wvs_scan_started"] = dict(id=439)
events["scan_wvs_scan_finished"] = dict(id=440)
events["scan_wvs_manual_browsing"] = dict(id=441)
events["scan_wvs_scan_resumed"] = dict(id=442)
events["vulnerability_marked_as"] = dict(id=500)
events["vulnerability_rediscovered"] = dict(id=501)
events["new_acumonitor_vulnerability"] = dict(id=502, notification=True)
events["report_asked"] = dict(id=600)
events["report_created"] = dict(
    id=601,
    notification=True,
    email_template=dict(
        subject=None,
        subject_template="report_done_subject.txt.jinja2",
        html_template="report_done.html.jinja2",
        txt_template="report_done.txt.jinja2",
    ),
)
events["export_asked"] = dict(id=602)
events["export_created"] = dict(id=603)
events["report_failed"] = dict(
    id=604, severity=EventSeverity.IMPORTANT, notification=True
)
events["export_failed"] = dict(
    id=605, severity=EventSeverity.IMPORTANT, notification=True
)
events["wvs_new_version"] = dict(id=700, notification=True)
events["worker_detached"] = dict(
    id=800, notification=True, severity=EventSeverity.INFORMATION
)
events["worker_not_responding"] = dict(
    id=801, notification=True, severity=EventSeverity.IMPORTANT
)


def get_event_severity(name):
    return events[name].get("severity", EventSeverity.NOTIFICATION)


def create_event(
    name,
    owner_id,
    user_id=None,
    data=None,
    resource_type=None,
    resource_id=None,
    severity=None,
    shard=None,
    inhibit_notification=False,
):
    event_descriptor = events[name]
    if user_id is not None:
        if user_id == owner_id:
            user_id = None
    if resource_id is not None:
        if isinstance(resource_id, int):
            resource_id = "%032x" % (resource_id,)
    if inhibit_notification:
        consumed = None
    else:
        consumed = False if event_descriptor.get("notification") else None
    event = dict(
        owner_id=owner_id,
        user_id=user_id,
        type_id=events[name]["id"],
        data=data,
        severity=severity
        if severity is not None
        else event_descriptor.get("severity", EventSeverity.NOTIFICATION),
        resource_type=resource_type,
        resource_id=resource_id,
        consumed=consumed,
    )
    email_template = event_descriptor.get("email_template")
    if email_template:
        mailer = settings.get("mailer_object")
        if mailer:
            mailer.enqueue(
                from_address=None,
                user_id=user_id if user_id else owner_id,
                shard=shard,
                data=dict(
                    setup_type=get_setup_type(),
                    event_name=name,
                    event_data=data,
                    severity=event["severity"],
                    resource_type=resource_type,
                    resource_id=resource_id,
                    shard=shard,
                ),
                subject=email_template.get("subject"),
                subject_template=email_template.get("subject_template"),
                html_template=email_template.get("html_template"),
                txt_template=email_template.get("txt_template"),
                frontend_url=settings.get("frontend_url"),
            )
    return EventsTable.insert().values(event)


class EventResources:
    user = 1
    child_user = 2
    target = 3
    group = 4
    scan = 5
    scan_session = 6
    report = 7
    worker = 8
    vulnerability = 9
    export = 10


EventsTable = Table(
    "events",
    metadata,
    Column("event_id", BIGINT, primary_key=True),
    Column(
        "owner_id",
        C_UUID,
        ForeignKey("users.user_id", ondelete="CASCADE"),
        nullable=False,
        index=True,
    ),
    Column(
        "user_id", C_UUID, ForeignKey("users.user_id", ondelete="SET NULL"), index=True
    ),
    Column("type_id", INTEGER, index=True),
    Column("severity", INTEGER, default=0),
    Column("created", DateTime(true), default="now()"),
    Column("resource_type", INTEGER, index=True),
    Column("resource_id", TEXT, index=True),
    Column("data", TYPE_JSONB),
    Column("consumed", BOOLEAN, index=True),
)


class EventRow(object):
    event_id = None
    owner_id = None
    user_id = None
    creator_id = None
    type_id = None
    consumed = None
    resource_type = None
    resource_id = None
    severity = None
    created = None

    def __str__(self):
        return "R_event[%s]" % (self.event_id,)

    def __repr__(self):
        return self.__str__()


mapper(EventRow, EventsTable)
